package com.jj.spb.spring_boot_mybatis.controller;

import com.alibaba.fastjson.JSON;
import com.jj.spb.spring_boot_mybatis.entity.Vote;
import com.jj.spb.spring_boot_mybatis.entity.VoteItem;
import com.jj.spb.spring_boot_mybatis.entity.VoteResult;
import com.jj.spb.spring_boot_mybatis.factory.ResultFactory;
import com.jj.spb.spring_boot_mybatis.service.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/VoteService")
public class VoteController {

    @Autowired
    VoteService service;

    @RequestMapping(value = "/queryVoteResult",method = RequestMethod.POST)
    public String queryVoteResult(@RequestParam(name = "id") Long id){
        List<VoteResult> results = service.queryVoteResultByVoteId(id);
        if(results == null){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"未找到投票结果");
        }
        return ResultFactory.buildResultBody(true, results,ResultFactory.ERROR_OK,"");
    }

    @RequestMapping(value = "/addVote",method = RequestMethod.POST)
    public String addVote(@RequestParam(name = "name")String name){
        Vote vote = new Vote();
        vote.setName(name);
        long id = service.saveVote(vote);
        if(id == 0){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"添加失败");
        }
        return ResultFactory.buildResultBody(true, id,ResultFactory.ERROR_OK,"添加成功");
    }

    @RequestMapping(value = "/addVoteItem",method = RequestMethod.POST)
    public String addVoteItem(@RequestParam(name = "voteId")Long voteId,
                             @RequestParam(name = "serialNum")int serialNum,
                             @RequestParam(name = "imageUrl")String imageUrl,
                             @RequestParam(name = "info")String info){
        if(!service.existVote(voteId)){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"该投票不存在");
        }
        if(service.existVoteItemBySerialNum(voteId,serialNum)){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"该投票的第 " + serialNum + " 已存在");
        }
        VoteItem item = new VoteItem();
        item.setVoteId(voteId);
        item.setImageUrl(imageUrl);
        item.setInfo(info);
        item.setSerialNum(serialNum);
        long id = service.saveVoteItem(item);
        if(id == 0){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"添加失败");
        }
        return ResultFactory.buildResultBody(true, id,ResultFactory.ERROR_OK,"添加成功");
    }

    @RequestMapping(value = "/upDataVoteItem",method = RequestMethod.POST)
    public String upDataVoteItem(@RequestParam(name = "voteId")Long voteId,
                              @RequestParam(name = "serialNum")int serialNum,
                              @RequestParam(name = "imageUrl")String imageUrl,
                              @RequestParam(name = "info")String info){
        if(!service.existVote(voteId)){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"该投票不存在");
        }
        if(!service.existVoteItemBySerialNum(voteId,serialNum)){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"该投票的第" + serialNum + "个条目不存在");
        }
        int oldId = service.findIdByVoteIdAndSerialNum(voteId,serialNum);
        VoteItem item = new VoteItem();
        item.setId(oldId);
        item.setVoteId(voteId);
        item.setImageUrl(imageUrl);
        item.setInfo(info);
        item.setSerialNum(serialNum);
        long id = service.saveVoteItem(item);
        if(id == 0){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"更新失败");
        }
        return ResultFactory.buildResultBody(true, id,ResultFactory.ERROR_FAILD,"更新成功");
    }

    @RequestMapping(value = "/getVoteDetail",method = RequestMethod.POST)
    public String getVoteDetail(@RequestParam(name = "voteId")String mVoteId){
        long voteId = Long.parseLong(mVoteId);
        if(!service.existVote(voteId)){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"该投票不存在");
        }
        Vote vote = service.findVote(voteId);
        List<VoteItem> items = service.findAllVoteItem(voteId);
        vote.setVoteItemList(items);
        return ResultFactory.buildResultBody(true, JSON.toJSONString(vote),ResultFactory.ERROR_FAILD,"");
    }

    @RequestMapping(value = "/addVoteResult",method = RequestMethod.POST)
    public String addVoteResult(@RequestParam(name = "voteId")Long voteId,
                              @RequestParam(name = "serialNums")String serialNums,
                              @RequestParam(name = "remark")String remark,
                              @RequestParam(name = "studentId")int studentId){
        if(!service.existVote(voteId)){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"该投票不存在");
        }
        VoteResult result = new VoteResult();
        result.setVoteId(voteId);
        result.setStudentId(studentId);
        result.setVoteItemIds(serialNums);
        result.setRemark(remark);
        int id = service.addOrUpdateVoteResult(result);
        if(id == 0){
            return ResultFactory.buildResultBody(false, "",ResultFactory.ERROR_FAILD,"投票失败");
        }
        return ResultFactory.buildResultBody(true, id,ResultFactory.ERROR_FAILD,"投票成功");
    }
}
